<?php defined('BASEPATH') or exit('No direct script access allowed');

class User_m extends MY_Model {	
	function __construct(){
		parent::__construct();
		$this->load->library('module_helper');
	}
	
	function wasInSystem(){
		if(isUserLogin()){
			redirect(site_url());
		}
	}
	
	function register_submit(){
		$USER_EXISTED1 = clone_ ( $this->user_entity->init('email', $this->input->post('email','')) );
		$USER_EXISTED2 = clone_ ( $this->user_entity->init('username', $this->input->post('username','')) );
		$SSO1 = clone_ ( $this->sso->init('email', $this->input->post('email','')) );
		$SSO2 = clone_ ( $this->sso->init('user_name', $this->input->post('username','')) );
		
		if($USER_EXISTED1 OR $SSO1){
			echo 'msg#This email was existed in system. You can not use this email.';
			exit();
		}
		if($USER_EXISTED2 OR $SSO2){
			echo 'msg#This username was existed in system. You can not use this username.';
			exit();
		}
		if( $this->input->post('captcha','') != $_SESSION['captcha'] ){
			echo 'err#captcha_error';
			exit();
		}
		
		$array_error = array();
		if( $this->phpvalidator->is_empty($this->input->post('username','')) ){
			$array_error[] = 'username_error';
		}
		if( $this->phpvalidator->is_empty($this->input->post('password','')) ){
			$array_error[] = 'password_error';
		}
		if( $this->input->post('password','') != $this->input->post('password2','') ){
			$array_error[] = 'password2_error';
		}
		if( !$this->phpvalidator->is_email($this->input->post('email','')) ){
			$array_error[] = 'email_error';
		}
		if( $this->phpvalidator->is_empty($this->input->post('fullname','')) ){
			$array_error[] = 'fullname_error';
		}
		if( $this->phpvalidator->is_empty($this->input->post('mobile','')) ){
			$array_error[] = 'mobile_error';
		}
		if(!empty($array_error)){
			echo 'err#'.implode('#',$array_error);
			exit();
		}
	
		if( !$this->phpvalidator->is_alphanumeric($this->input->post('username','')) ){
			echo 'msg#Username only accept alpha character and numeric';
			exit();
		}
		
		$uLen = strlen($this->input->post('username',''));
		$pLen = strlen($this->input->post('password',''));
		if($uLen<6 OR $uLen >8){
			echo 'msg#Username only from 6 to 8 characters';
			exit();
		}
		
		if($pLen<6 OR $pLen >8){
			echo 'msg#Password only from 6 to 8 characters';
			exit();
		}
		
		//save
		$newUserId = $this->user_entity->createNewUserEntity($this->input->post('username',''));
		$USER = clone_( $this->user_entity->init('id', $newUserId) );
		
		$USER->setEmail($this->input->post('email',''));
		$USER->setPassword($this->input->post('password',''));
		$USER->setFullName($this->input->post('fullname',''));
		$USER->setCity($this->input->post('city',''));
		$USER->setState($this->input->post('state',''));
		$USER->setCountry($this->input->post('country',''));
		$USER->setCountryCode($USER->getCountryCode($this->input->post('country','')));
		$USER->setMobile($this->input->post('mobile',''));
		$USER->setCreatedTime(time());
		$USER->setUpdatedTime(time());
		$USER->setCoin(0);
		$USER->setBonus(0);
		$USER->setCash(0);
		$USER->setReferralCode($this->digit->generateReferralCode());
		//$USER->setStatus('activated');
		$USER->setStatus('pending');
		
		if( strtolower( $this->input->post('country','') ) == 'india'){
			$USER->setCurrencyCode('inr');
		}else{
			$USER->setCurrencyCode('inr');
		}
		$USER->update();
		
		if(isset($_SESSION['refer_code'])){
			$UR = clone_( $this->user_entity->init('referCode', $_SESSION['refer_code']) );
			if($UR){
				$bonus = getBonusCoint('referFriend');
				$UR->setBonus( $UR->getBonus() + $bonus );
				$UR->setCoin( $UR->getCoin() + $bonus );
				$UR->update();
				$USER->setUserRefferId($UR->getId());
				$USER->update();
			}
			unset($_SESSION['refer_code']);
		}
		
		$this->email_sender->_sendEmailToUser_verificationEmail($newUserId);
		
		echo 'ok';
		exit();
	}	
	
	function login(){
		if(isUserLogin()){
			echo 'Logon';exit();
		}
		
		if(isset($_GET['ism']) AND $_GET['ism'] == 'GET'){
			$username = $this->input->get('username','');  
			$password = $this->input->get('password','');  
		}else{
			$username = $this->input->post('username','');  
			$password = $this->input->post('password','');  
		}
		$ipAdd			= $this->session->userdata('ip_address');
		
		$SSO_USER = $this->sso->init('user_name',$username);
		if($SSO_USER){	
		
			if($SSO_USER->getPassword() == ($password) AND $SSO_USER->getStatus() == 'Ok' ){
				//update or insert new into local users table
				$USER = $this->user_entity->init('username', $username);	 
				if($USER){
					$USER->setPassword($SSO_USER->getPassword());
					$USER->setEmail($SSO_USER->getEmail());
					$USER->setFullName($SSO_USER->getFullName());	
					$USER->setCity($SSO_USER->getCityName());
					$USER->setState($SSO_USER->getStateName());
					$USER->setCountryCode($SSO_USER->getCountryCode());
					$USER->setCountry($USER->getCountryName($SSO_USER->getCountryCode()));
					$USER->setMobile($SSO_USER->getMobileNo());
					$USER->setCreatedTime( convertToTimeStampFromFormat($SSO_USER->getDOJ(), 'Y-m-d H:i:s' ));
					$USER->update();
				}else{
					$newid = $this->user_entity->createNewUserEntity($username);
					$USER = $this->user_entity->init('id', $newid);
					$USER->setPassword($SSO_USER->getPassword());
					$USER->setEmail($SSO_USER->getEmail());
					$USER->setFullName($SSO_USER->getFullName() );	
					$USER->setCity($SSO_USER->getCityName());
					$USER->setState($SSO_USER->getStateName());
					$USER->setCountryCode($SSO_USER->getCountryCode());
					$USER->setCountry($USER->getCountryName($SSO_USER->getCountryCode()));
					$USER->setMobile($SSO_USER->getMobileNo());
					$USER->setCreatedTime( convertToTimeStampFromFormat($SSO_USER->getDOJ(), 'Y-m-d H:i:s' ));
					$USER->setStatus('activated');
					$USER->setCurrencyCode('inr');
					
					$USER->update();
				}	
				
				if( strlen( $USER->getReferralCode() ) <2){
					$USER->setReferralCode($this->digit->generateReferralCode());
				}
				$USER->setLoginIp($ipAdd);
				$USER->update();
				
				$arrayUserInfo = array('id'=>$USER->getId(),'email'=>$USER->getEmail(), 'username'=>$USER->getUserName(), 'name'=>$USER->getFullName());
				
				$this->session->set_userdata(array( 'account'=>$arrayUserInfo ));	
				$this->loginOther();
				
				echo 'ok';
				exit();
			}else{
				echo 'The password did not match or account was in-actived';exit();
			}
		}else{
			//check if user existed in local site , insert into general site db else login false
			$USER_EXISTED = $this->user_entity->init('username', $username);
			if($USER_EXISTED){
				if( $password == $USER_EXISTED->getPasswd() AND $USER_EXISTED->getStatus() == 'activated' )	// 
				{
					//$ipAdd			= $this->session->userdata('ip_address');
					
					$newid = $this->sso->createNewUser();
					if($newid){
						$SSO = $this->sso->init('user_id',$newid);
						
						$SSO->setUserName($USER_EXISTED->getUserName());
						$SSO->setPassword(($password));
						$SSO->setEmail($USER_EXISTED->getEmail());
						$SSO->setFullName($USER_EXISTED->getFullName());
						$SSO->setDOJ(sysDateTimeFormat($USER_EXISTED->getCreatedTime(),'Y-m-d H:i:s'));
						$SSO->setCityName($USER_EXISTED->getCity());
						$SSO->setCountryCode($USER_EXISTED->getCountryCode());
						$SSO->setStateName($USER_EXISTED->getState());
						$SSO->setMobileNo($USER_EXISTED->getMobile());
						$SSO->update();
					}
					
					if( strlen( $USER_EXISTED->getReferralCode() ) <2){
						$USER_EXISTED->setReferralCode($this->digit->generateReferralCode());
					}
					$USER_EXISTED->setLoginIp($ipAdd);
					$USER_EXISTED->update();
					
					$arrayUserInfo = array('id'=>$USER_EXISTED->getId(),'email'=>$USER_EXISTED->getEmail(), 'username'=>$USER_EXISTED->getUserName(), 'name'=>$USER_EXISTED->getFullName());
					$this->session->set_userdata(array('account'=>$arrayUserInfo));	
					
				    $this->loginOther();
					echo 'ok';
					exit();
				}else
				{
					echo 'The password did not match or account was in-actived';exit();
				}
			}else
			{
				echo 'The username and password did not match.';exit();
			}
		
		}
	}
	
	
	function loginOther(){
		$USER = $this->user_entity->init('id',getUserId());
		$USER->setSid($this->module_helper->getTokenId());
		$USER->update();
		$uname = $USER->getUserName();
		$passwd = sha1($USER->getPasswd());
		
		$_SESSION['autoLogin'] = array(
			"http://www.clicxa.com/login.php?exlogin=yes&user_name={$uname}&password={$passwd}",
			"http://www.clicxabids.com/login.php?exlogin=yes&user_name={$uname}&password={$USER->getPasswd()}"
		);
	}
	
	function logoutOther(){
		$_SESSION['autoLogout'] = array(
			"http://www.clicxa.com/logout.php",
			"http://www.clicxabids.com/logout.php"
		);
	}
	
	function logout(){
		if(isUserLogin()){
			$this->session->unset_userdata('account');
			$this->logoutOther();
		}
		redirect(site_url());
	}
	
	function profile_submit(){
		$USER = $this->user_entity->init('id',intval($_POST['user_id']));
		if($USER->getId() != getUserId() ){
			echo "msg#Something is error here.";exit();
		}
		$myavatar = $USER->getAvatar();
		
		if(isset($_FILES["avatar"]) AND !empty($_FILES["avatar"]['name'])) {
			if(($_FILES["avatar"]['size']/1000000) > allowMaxFileSize() ){
				$myavatar = '';
			}else{
				$myavatar = $this->module_helper->uploadFile( 
										$_FILES["avatar"]['tmp_name'],
										$_FILES["avatar"]['name'],
										$USER->getFolderFileStorage(),
										allowExtensionPictureUpload()
									);
			}	
		}
		
		if(empty($_POST['fullname'])){
			echo "err#fullname_error";exit();
		}
		if(empty($_POST['mobile'])){
			echo "err#mobile_error";exit();
		}
		
		$USER->setFullName($_POST['fullname']);
		$USER->setCity($_POST['city']);
		$USER->setState($_POST['state']);
		$USER->setCountry($_POST['country']);
		$USER->setCountryCode($USER->getCountryCode_($_POST['country']));
		$USER->setMobile($_POST['mobile']);
		$USER->setUpdatedTime(time());
		$USER->setAvatar($myavatar);
		
		$USER->update();
		
		$SSO = $this->sso->init('user_name', $USER->getUserName());
		$SSO->setFullName($USER->getFullName());
		$SSO->setCityName($USER->getCity());
		$SSO->setCountryCode($USER->getCountryCode());
		$SSO->setStateName($USER->getState());
		$SSO->setMobileNo($USER->getMobile());
		
		$SSO->update();
		
		echo 'msg#Save profile successfully.';exit();
	}
	
	function change_password_submit(){
		$USER = $this->user_entity->init('id',intval($_POST['user_id']));
		$SSO = $this->sso->init('user_name', $USER->getUserName());
		
		if($USER->getId() != getUserId()){
			echo "msg#Something is error here.";exit();
		}
		if($USER->getPasswd() != $_POST['old_password']){
			echo "err#old_password_error";exit();
		}
		if( $SSO->getPassword() != ($_POST['old_password']) ){
			echo "err#old_password_error";exit();
		}
		if(empty($_POST['new_password'])){
			echo "err#new_password_error";exit();
		}
		if($_POST['new_password'] != $_POST['new_password2']){
			echo "err#new_password2_error";exit();
		}
		
		$pLen = strlen($_POST['new_password']);
		if($pLen<6 OR $pLen >8){
			echo 'msg#Password only from 6 to 8 characters';
			exit();
		}
		
		$USER->setPassword($_POST['new_password']);
		$USER->update();
		
		$SSO->setPassword(($_POST['new_password']));
		$SSO->update();
		
		echo "msg#Change password successfully.";exit();
	}
	
	function change_email_submit(){
		if( !$this->phpvalidator->is_email($this->input->post('email','')) ){
			echo 'err#email_error';exit();
		}
		$USER = clone_( $this->user_entity->init('id',intval($_POST['user_id'])) );
		$USER_EXISTED = clone_( $this->user_entity->init('email',($_POST['email'])) );
		
		$SSO = clone_( $this->sso->init('user_name', $USER->getUserName()) );
		$SSO_EXISTED = clone_( $this->sso->init('email', $_POST['email']) );
		
		if($SSO_EXISTED AND $SSO->getUserId() != $SSO_EXISTED->getUserId()){
			echo "err#new_email_error";exit();
		}
		
		if($USER->getId() != getUserId()){
			echo "msg#Something is error here.";exit();
		}
		if($USER_EXISTED AND $USER->getId() != $USER_EXISTED->getId()){
			echo "err#new_email_error";exit();
		}
		
		if(!$USER_EXISTED){
			$USER->setEmail($_POST['email']);
			$USER->update();
		}
		
		$SSO->setEmail(($_POST['email']));
		$SSO->update();
		
		echo "msg#Change Email successfully.";exit();
	}
	
	function submit_shipping_info(){
		$SHIPPING = $this->shipping_entity->init('user_id',$_POST['user_id']);
				
		if(!$SHIPPING){
			$id = $this->shipping_entity->createNewEntity();
			$SHIPPING = $this->shipping_entity->init('id', $id);
			$SHIPPING->setCourierName($_POST['shipping_info']);
			$SHIPPING->setUserId($_POST['user_id']);
			 
			$SHIPPING->update();
		}else{
			$SHIPPING->setCourierName($_POST['shipping_info']);
			$SHIPPING->update();
		}
		
		echo "msg#Shipping info updated successfully.";exit();
	}
	
	function refer_friend_submit(){
		$USER =  $this->user_entity->init('id',intval($_POST['user_id'])) ;
		if( !$this->phpvalidator->is_email($this->input->post('email','')) ){
			echo "err#email_error";exit();
		}
		if( ! $this->input->post('name','') ){
			echo "err#name_error";exit();
		}
		echo "msg#Sent email successfully.#";
		$this->email_sender->_sendEmailToUser_invite(	
							$USER->getId(), 
							$this->input->post('email',''),
							$this->input->post('name',''), 
							substr( $this->input->post('message',''), 0, 500 )
						);
		exit();				
	}
	
	function submit_subscription(){
		$USER = $this->user_entity->init('id',getUserId()) ;
		$USER->setEmailSubscription($this->input->post('email_subscription',''));
		$USER->setSmsSubscription($this->input->post('sms_subscription',''));
		$USER->update();
		
		echo "msg#Updated successfully.";exit();
	}
	
	function redeem_coupon_submit(){
		$USER = $this->user_entity->init('id',getUserId()) ;
		$code = $_POST['code'];
		$COUPON = $this->coupon_entity->init('code',$code);
		if(! $COUPON){
			echo "err#This code is not existed.";exit();
		}
		if( $COUPON->getStartDate() > time() || $COUPON->getEndDate() < time() ){
			echo "err#The coupon code is time out.";exit();
		}
		
		if($COUPON->getMode() == 'single'){
			if($COUPON->getValidity() != 'pending'){
				echo "err#This code is already used.";exit();
			}
		
			$USER->setCoin( $USER->getCoin() + $COUPON->getCoin() );
			$USER->update();
			$COUPON->setUserRedeem($USER->getId());
			$COUPON->setValidity('redeem');
			$COUPON->update();
			
			echo "msg#You have added ".$COUPON->getCoin()." coins to your account.";
			exit();
		}else{
			$userIdArray = array_filter(explode(',',$COUPON->getUserRedeem()));
			if(in_array($USER->getId(), $userIdArray)){
				echo "err#This code is already used.";exit();
			}else{
				$USER->setCoin( $USER->getCoin() + $COUPON->getCoin() );
				$USER->update();
				
				array_push($userIdArray, $USER->getId());
				$COUPON->setUserRedeem(implode(',',$userIdArray));
				$COUPON->setValidity('redeem');
				$COUPON->update();
				
				echo "msg#You have added ".$COUPON->getCoin()." coins to your account.";
				exit();
			}
		}
	}
	
	function forgot_details_submit(){
		$captcha = $this->input->post('captcha', TRUE);
		$email = $this->input->post('email', TRUE);
		
		if( $captcha != $_SESSION['captcha']){
			echo "msg#Captcha do not match.";
			exit();
		}
		
		$USER = $this->user_entity->init('email', $email);
		if(!$USER){
			echo "msg#Email is not existed in system.";
			exit();
		}
		
		$this->email_sender->_sendEmailToUser_forgot_details( $USER->getId() );
		echo "ok";
		exit();
	}
	
	function deposit_funds_submit(){
		$method = $this->input->get('method',TRUE);
		$amount = intval($this->input->get('amount',TRUE));
		
		if( !isUserLogin() || $amount <= 0 || !in_array($method , array('ccavenue','paypal')) ){
			redirect(site_url());exit();
		}
		 
		$_SESSION['deposit_amount'] = $amount;
		if($method == 'paypal'){
			redirect(site_url("cricket/paypal/process"));exit();
		}else{
			
		}
	}
	
	function submit_addcoins_paypal(){
		$package_id = intval($this->input->get('pid',TRUE));
		$PACKAGE = $this->coin_packages_entity->init('id', $package_id);
		
		if(!$PACKAGE || !isUserLogin()){
			redirect(site_url());exit();
		}
		redirect(site_url("cricket/paypal/process_addcoins?pid=$package_id"));exit();
	}
	
	function verify_cod_submit(){
		$USER = $this->user_entity->init('id',getUserId()) ;
		$array_error = array();
		
		$cod_mobile = $this->input->post('cod_mobile','');
		$cod_address = $this->input->post('cod_address','');
		$cod_city = $this->input->post('cod_city','');
		$cod_state = $this->input->post('cod_state','');
		$cod_postalcode = $this->input->post('cod_postalcode','');
		
		if( $this->phpvalidator->is_empty($cod_mobile) || !$this->phpvalidator->is_length($cod_mobile,10)){
			$array_error[] = 'cod_mobile_error';
		}
		if( $this->phpvalidator->is_empty($cod_address) ){
			$array_error[] = 'cod_address_error';
		}
		if( $this->phpvalidator->is_empty($cod_city) ){
			$array_error[] = 'cod_city_error';
		}
		if( $this->phpvalidator->is_empty($cod_state) ){
			$array_error[] = 'cod_state_error';
		}
		if( !$this->phpvalidator->is_natural($cod_postalcode) || !$this->phpvalidator->is_length($cod_postalcode,6) ){
			$array_error[] = 'cod_postalcode_error';
		}
		
		if(!empty($array_error)){
			echo 'err#'.implode('#',$array_error);
			exit();
		}
		$verify_code = $this->digit->rand_digit(6);
		
		$USER->setCodMobile($cod_mobile);
		$USER->setCodAddress($cod_address);
		$USER->setCodStatus($verify_code);
		$USER->setCodCity($cod_city);
		$USER->setCodState($cod_state);
		$USER->setCodePostalCode($cod_postalcode);
		
		$USER->update();
		sendSMS($USER->getCodMobile(), "YOUR VERIFICATION CODE: $verify_code");
		echo "ok";exit();
	}
	
	function verify_mobile_submit(){
		$USER = $this->user_entity->init('id',getUserId()) ;
		$array_error = array();
		$code = $this->input->post('verification_code','');
		
		if( $this->phpvalidator->is_empty($code) ){
			$array_error[] = 'verification_code_error';
		}
		if(!empty($array_error)){
			echo 'err#'.implode('#',$array_error);
			exit();
		}
		
		if($USER->getCodStatus() != $code){
			echo "msg#Your verification code is not matched.";
			exit();
		}else{
			$USER->setCodStatus('verify');
			$USER->update();
			echo "ok";exit();
		}
	}
	
	function add_cod_submit(){
		$USER = $this->user_entity->init('id',getUserId()) ;
		
		if($USER->getCodStatus() != 'verify'){
			$_SESSION['package_id'] = intval($_POST['pid']);
			echo 'need_verify';exit();
		}else{
			if(isset($_SESSION['package_id'])){
				unset($_SESSION['package_id']);
			}
		}
		
		$count_pending = count( $this->db->where(array('user_id'=>$USER->getId(), 'status'=>'pending'))->get(TBL_COD)->result() );
		if($count_pending >= 2){
			echo "Not valid. Your pending purchase is greater than 2.";
			exit();
		}
		$PACKAGE = $this->coin_packages_entity->init('id',intval($_POST['pid']));
		
		$newId = $this->cod_data_entity->createNewEntity();
		$COD = $this->cod_data_entity->init('id',$newId);
		
		$COD->setUserId($USER->getId());
		$COD->setPrice($PACKAGE->getCost());
		$COD->setCoin($PACKAGE->getCoin());
		$COD->setPackageId($PACKAGE->getId());
		$COD->setStatus('pending');
		$COD->setCode($this->digit->generateCODcode());
		$COD->setRedeemStatus("pending");
		
		$COD->update();
		echo "ok";exit();
	}
	
	function change_cod_info_submit(){
		$USER = $this->user_entity->init('id',getUserId()) ;
		$array_error = array();
		
		$mobile = $this->input->post('cod_mobile','');
		$cod_address = $this->input->post('cod_address','');
		$cod_city = $this->input->post('cod_city','');
		$cod_state = $this->input->post('cod_state','');
		$cod_postalcode = $this->input->post('cod_postalcode','');
		
		if( $this->phpvalidator->is_empty($mobile) || !$this->phpvalidator->is_length($mobile,10) ){
			$array_error[] = 'cod_mobile_error';
		}
		if( $this->phpvalidator->is_empty($cod_address) ){
			$array_error[] = 'cod_address_error';
		}
		if( $this->phpvalidator->is_empty($cod_city) ){
			$array_error[] = 'cod_city_error';
		}
		if( $this->phpvalidator->is_empty($cod_state) ){
			$array_error[] = 'cod_state_error';
		}
		if( !$this->phpvalidator->is_natural($cod_postalcode) || !$this->phpvalidator->is_length($cod_postalcode,6) ){
			$array_error[] = 'cod_postalcode_error';
		}
		
		if(!empty($array_error)){
			echo 'err#'.implode('#',$array_error);
			exit();
		}
		if($mobile != $USER->getCodMobile()){
			$USER->setCodMobile($mobile);
			$verify_code = $this->digit->rand_digit(6);
			$USER->setCodStatus($verify_code);
		}
		$USER->setCodAddress($cod_address);
		$USER->setCodCity($cod_city);
		$USER->setCodState($cod_state);
		$USER->setCodePostalCode($cod_postalcode);
		
		$USER->update();
		echo "msg#Updated successfully.";exit();
	}
	
	function redeem_cod_coupon_submit(){
		$USER = $this->user_entity->init('id',getUserId()) ;
		$code = $_POST['code'];
		$COD = $this->cod_data_entity->init('code',$code);
		if(! $COD){
			echo "err#This code is not existed.";exit();
		}
		
		if($COD->getRedeemStatus() != 'pending'){
			echo "err#This code is already used.";exit();
		}
	
		$USER->setCoin( $USER->getCoin() + $COD->getCoin() );
		$USER->update();
		$COD->setRedeemStatus('redeem');
		$COD->setStatus('completed');
		$COD->update();
		
		echo "msg#You have added ".$COD->getCoin()." coins to your account.";
		exit();
	}
	
	
	//deprecated
	function add_coins_submit(){
		$package_id = intval($this->input->post('pid',TRUE));
		$PACKAGE = $this->coin_packages_entity->init('id', $package_id);
		$USER = $this->user_entity->init('id',getUserId()) ;
		
		$myCashUSD = $USER->convertToUSD();
		$myPayment = $PACKAGE->getCost();
		if($myPayment > $myCashUSD){
			echo "Invalid transaction.";exit();
		}
		
		$myCashUSDreturn = $myCashUSD - $myPayment;
		$myCash = changeValueOfMoney( $myCashUSDreturn, $USER->getCurrencyCode(), 'usd' );
		$USER->setCash($myCash);
		$USER->setCoin( $USER->getCoin() + $PACKAGE->getCoin() );
		$USER->update();
		echo "ok";
		exit();
	}
	
	function add_coins_from_bonus(){
		$USER = $this->user_entity->init('id',getUserId()) ;
		$USER->setCoin( $USER->getCoin() + $USER->getBonus() );
		$USER->setBonus(0);
		$USER->update();
		echo "ok";
		exit();
	}
	//////////
	
}